#!/usr/bin/env node

const fs = require('fs'),
      join = require('path').join,
      src = process.argv[2];
const err = console.error;

if(process.argv.length != 3){
  err('命令行参数不正确');
  process.exit(1);
}

if(typeof(src) === 'undefined'){
  err('请指定要删除的文件名或目录名');
  process.exit(2);
}

if(!fs.existsSync(src)){
  err(`${src} not exist`);
  process.exit(3);
}

if(fs.statSync(src).isFile()){
  fs.unlinkSync(src);
}

if(fs.statSync(src).isDirectory()){
  deleteDir(src);
}

function deleteDir(folder){
  //读取目录下的文件，存到files数组
  let files = fs.readdirSync(folder);
  
  for(let i=0; i<files.length; i++){
    let file = join(folder,files[i]);

    if(fs.statSync(file).isFile()){
      fs.unlinkSync(file);
      continue;
    }

    if(fs.statSync(file).isDirectory()){
      //递归
      deleteDir(file);
    }
  }

  fs.rmdirSync(folder);

}



